# coding:utf-8
import os
import time
import multiprocessing
from multiprocessing import Manager


def work(count, lock):
    try:
        lock.acquire()
        print(count, os.getpid())
        # time.sleep(2)
    finally:
        lock.release()
        return 'result is %s, pid is %s' % (count, os.getpid())


if __name__ == '__main__':
    pool = multiprocessing.Pool(5)
    manager = Manager()
    lock = manager.Lock()
    # 20 个任务提交到进程池(size = 5)
    results = []
    for i in range(20):
        result = pool.apply_async(func=work, args=(i, lock))
        results.append(result)

    pool.close()
    pool.join()
    for res in results:
        print(res.get())
    # print(result)
    # time.sleep(20)
